package com.qf.listen;

import com.qf.Constant.RabbitMqConstant;
import com.qf.entity.Msg;
import com.qf.util.HttpUitls;
import com.rabbitmq.client.Channel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 你的名字
 * @Date: 2021/07/14/19:34
 * @Description:
 */
@Component
@ConfigurationProperties(prefix = "sms")
@Slf4j
@Data
public class SmgListen {
   public String path ;
    String host  ;
    String appCode;
    @RabbitListener(queues = RabbitMqConstant.SMS_QUEUE)
    public void SendMsg(Msg msg, Channel channel, Message message){
        Map<String,String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " +appCode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", msg.getPhone());
        querys.put("templateId", msg.getTemplateId());
        querys.put("value", msg.getCode());
        Map<String,String> bodys = new HashMap<String, String>();


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUitls.doPost(host, path, "POST", headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
